<?php

namespace App\Console\Commands;

use App\Model\Distributor;
use App\Model\MaibaoQueue;
use App\User;
use Carbon\Carbon;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Maibao extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:maibao';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        while (true) {
            try {
                DB::beginTransaction();
                //取出队列
                $queue = MaibaoQueue::where('status', 0)->first();
                if (is_null($queue)){
                    DB::rollBack();
                    sleep(10);//休息一会
                    continue;
                }
                //获取经销商
                $region_id = 0;
                try {
                    $http = new \GuzzleHttp\Client();
                    $response = $http->request('GET', 'http://ip.taobao.com/service/getIpInfo.php', [
                        'query' => ['ip' => $queue->ip]
                    ]);
                    $res = json_decode($response->getBody(), true);
                    $region_id = $res['data']['region_id'];
                }catch (GuzzleException $e) {
                    $region_id = 0;
                }
                $distributor = Distributor::where('region_id', $region_id)->lockForUpdate()->first();
                $user = User::where('id', $queue->user_id)->lockForUpdate()->first();
                if (is_null($distributor)) {
                    $queue->status = 1;
                    $queue->save();
                    $user->increment('maibao_real', $queue->amount);
                    DB::table('maibao_distributor_log')->insert([
                        'user_id' => $queue->user_id,
                        'maibao_id' => $queue->maibao_id,
                        'cate' => 1,
                        'distributor_id' => $distributor->id,
                        'type' => 1,
                        'amount' => $queue->amount,
                        'amount_real' => 0,
                        'phone' => $user->phone,
                        'name' => $user->cards?$user->cards->user_name:'',
                        'ip' => $queue->ip,
                        'region' => $distributor->region,
                        'region_id' => $region_id,
                        'content' => '用户充值',
                        'created_at' => Carbon::now()
                    ]);
                    DB::commit();
                    continue;
                }
                if ($distributor->maibao <= 0) {
                    //当前所选的经销商没有脉宝了
                    $user->increment('maibao_real', $queue->amount);
                    $queue->status = 1;
                    $queue->save();
                    DB::table('maibao_distributor_log')->insert([
                        'user_id' => $queue->user_id,
                        'maibao_id' => $queue->maibao_id,
                        'cate' => 1,
                        'distributor_id' => $distributor->id,
                        'type' => 1,
                        'amount' => $queue->amount,
                        'amount_real' => 0,
                        'phone' => $user->phone,
                        'name' => $user->cards?$user->cards->user_name:'',
                        'ip' => $queue->ip,
                        'region' => $distributor->region,
                        'region_id' => $region_id,
                        'content' => '用户充值',
                        'created_at' => Carbon::now()
                    ]);
                    DB::commit();
                    continue;
                }
                $amount = $queue->amount;
                if ($distributor->maibao < $queue->amount) {
                    //经销商剩余脉宝不够充值
                    $amount = $queue->amount - $distributor->maibao;
                }

                $distributor->decrement('maibao', $amount);
                $distributor->increment('reward', $amount);
                $distributor->increment('reward_all', $amount);
                $user->increment('maibao_real', $queue->amount - $amount);
                $queue->status = 1;
                $queue->save();
                //记录日志
                DB::table('maibao_distributor_log')->insert([
                    'user_id' => $queue->user_id,
                    'cate' => 1,
                    'distributor_id' => $distributor->id,
                    'type' => 1,
                    'amount' => $amount,
                    'amount_real' => $amount,
                    'phone' => $user->phone,
                    'name' => $user->cards?$user->cards->user_name:'',
                    'ip' => $queue->ip,
                    'region' => $distributor->region,
                    'region_id' => $region_id,
                    'content' => '用户充值',
                    'created_at' => Carbon::now()
                ]);
                DB::commit();
            } catch (\Exception $e) {
                Log::error($e);
                DB::rollBack();
            }
        }
    }
}
